﻿using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Serilog;
using SheepStorage.API.Application.Command.SheepPurchaseCommand;
using SheepStorage.API.Baseclass;
using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;

namespace SheepStorage.API.Controllers
{
    /// <summary>
    /// 羊只采购
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class SheepPurchaseController : ControllerBase
    {
        private readonly IMediator _mediator;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="mediator"></param>
        public SheepPurchaseController(IMediator mediator)
        {
            _mediator = mediator;
        }


        /// <summary>
        /// 羊只采购列表
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<PageOutput<SheepPurchase>>> GetSheepPurchase(SheepPurchaseSelectCommand command)
        {
            try
            {
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                Log.Error("获取羊只采购列表出错" + ex.Message);
                throw;
            }
            
        }


        /// <summary>
        /// 羊只采购添加
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> SheepPurchaseAdd(SheepPurchaseCreateCommand command)
        {
            try
            {
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                Log.Error("羊只采购添加出错" + ex.Message);
                throw;
            }
            
        }

        /// <summary>
        /// 批量删除/逻辑删除
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpDelete]
        public async Task<Results<int>> DeleteSheepPurchase(SheepPurchaseDeleteCommand command)
        {
            try
            {
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                Log.Error("逻辑删除出错" + ex.Message);
                throw;
            }
            
        }

        /// <summary>
        /// 羊只采购单个数据
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<Results<SheepPurchase>> GetSheepPurchaseInfo([FromQuery] SheepPurchaseFillCommand command)
        {
            try
            {
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                Log.Error("获取羊只采购单个数据出错" + ex.Message);
                throw;
            }
            
        }

        /// <summary>
        /// 羊只采购修改
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<Results<int>> UpdateSheepPurchase(SheepPurchaseUpdateCommand command)
        {
            try
            {
                return await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                Log.Error("羊只采购修改出错" + ex.Message);
                throw;
            }
            
        }

    }
}
